/*
 * Author:  Andy R. Schmitz
 * ClemID:  ARSCHMI
 * email:   Andy@ARSchmitz.com
 * MadeFor: CpSc 210 - Lab - Lab4
 * Date:    16 Sept 2010
 * License: Public Domain
 */
#include "../hdrs/list.h"

/** List node  **/
/* typedef struct node_type {
 *   void *objectP;
 *   struct node_type *next;
 * } node_t;
 *
 * typedef struct list_type {
 *   node_t *front;
 *   node_t *back;
 *   node_t *current;
 * } list_t;
 */

list_t *createList() {
    list_t *result = (list_t *) malloc(sizeof(list_t));
    result->front   = NULL;
    result->back    = NULL;
    result->current = NULL;
    return result;
}

void addObject(list_t *list, void *objectP) {
  node_t *result = (node_t *) malloc(sizeof(node_t));
  result->objectP = objectP;
  result->next = NULL;
  
  if(list->back == NULL){
    list->front   = result;
    list->current = result;
  } else {
    list->back->next = result;
  }
  list->back = result;
}

void *getObject(list_t *list) {
  if(list->current == NULL){
    list->current = list->front;
    return NULL;
  }
  void *result = list->current->objectP;
  list->current = list->current->next;
  return result;
}

/****************
 *  HOMEWORK 4  *
 ****************
 */
void *deleteObject(list_t *list, int element){
  node_t *prev = NULL;
  node_t *del = list->front;
  for(;element>0;element--){
    prev = del;
    del = del->next;
    if(del == NULL)
      return NULL; //element past list_length
  }
  if(del == list->front){   //Special Cases - Front
    list->front = del->next;}
  if(del == list->back){    //Special Cases - Back
    list->back = prev;}
  if(del == list->current){ //Special Cases - Current
    list->current = del->next;}
  if(prev != NULL){
    prev->next = del->next;}  //Node removed from the loop
  void *data = del->objectP;
  free(del);
  return data;
}

void resetList(list_t *list) {
  list->current = list->front;
}

int endList(list_t *list) {
  return list->current == NULL;
}


//Andy R. Schmitz
